package com.app.twth.ui.b2cpick.category;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.R;
import com.app.twth.databinding.ItemB2cpickCategoryDetailsListBinding;
import com.app.twth.interfaces.MyStringOnClickListener;
import com.app.twth.interfaces.MyTwoStoryOnClickListener;
import com.app.twth.ui.b2cpick.B2CPickDetails;

import java.util.ArrayList;
import java.util.List;


public class B2CPickCategoryDetailsAdapter extends RecyclerView.Adapter<B2CPickCategoryDetailsAdapter.MyViewHolder> {
    private String TAG = getClass().getSimpleName();
    private int one;
    private Context mContext;
    private LayoutInflater mInflater;
    private MyStringOnClickListener lpnListener;
    private MyTwoStoryOnClickListener skuListener;
    private List<B2CPickDetails.SkuDetailListDTO> data = new ArrayList<>();

    public B2CPickCategoryDetailsAdapter(Context mContext, List<B2CPickDetails.SkuDetailListDTO> data, MyTwoStoryOnClickListener skuListener, int one, MyStringOnClickListener lpnListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.skuListener = skuListener;
        this.one = one;
        this.lpnListener = lpnListener;
    }

    public void setData(List<B2CPickDetails.SkuDetailListDTO> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemB2cpickCategoryDetailsListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemB2cpickCategoryDetailsListBinding itemBinding;

        public MyViewHolder(ItemB2cpickCategoryDetailsListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            B2CPickDetails.SkuDetailListDTO locationsDTO = data.get(position);
            itemBinding.tvItem.setText(locationsDTO.getSku() + "(" + locationsDTO.getPendingQty() + "/" + locationsDTO.getPickedQty() + ")");
            itemBinding.layoutItem.setOnClickListener(v -> {
                Log.e(TAG, "ccccccc");
                if (skuListener != null) {
                    skuListener.OnClick(one, position);
                }
            });
            List<B2CPickDetails.IbLpnListDTO> lpns = locationsDTO.getIbLpnList();
            B2CPickCategoryLpnAdapter lpnAdapter = new B2CPickCategoryLpnAdapter(mContext, lpns, lpnListener);
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            itemBinding.recyclerviewLpn.setLayoutManager(layoutManager);
            itemBinding.recyclerviewLpn.setAdapter(lpnAdapter);

            List<B2CPickDetails.LocationLpnDTO> pickLocationDetails = locationsDTO.getLocationLpnDTOS();
            B2CPickCategoryLocationLPNAdapter skuAdapter = new B2CPickCategoryLocationLPNAdapter(mContext, pickLocationDetails, lpnListener);
            LinearLayoutManager skuLayoutManager = new LinearLayoutManager(mContext);
            skuLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            itemBinding.recyclerviewSku.setLayoutManager(skuLayoutManager);
            itemBinding.recyclerviewSku.setAdapter(skuAdapter);


            if (locationsDTO.isClick()) {
                itemBinding.ivDown.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_up));
                itemBinding.recyclerviewSku.setVisibility(View.VISIBLE);
                itemBinding.viewLine.setVisibility(View.VISIBLE);
            } else {
                itemBinding.recyclerviewSku.setVisibility(View.GONE);
                itemBinding.viewLine.setVisibility(View.GONE);
                itemBinding.ivDown.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_down));
            }

        }
    }

}
